﻿Imports System.Xml
Imports System.IO

Public Class XMLSoftWare
    Private Shared _filePath As String = Util.GetSoftWareXml
    Private Shared _xmldoc As XmlDocument

    Public Sub New()
        If _xmldoc Is Nothing Then
            _xmldoc = New XmlDocument
            If Not File.Exists(_filePath) Then
                Util.CreateSoftWareTemple()
            End If
            _xmldoc.Load(_filePath)
        End If
    End Sub

    Public Function Create(ByVal ty As CSoftWare) As Boolean

        Dim dwt As XmlElement = _xmldoc.CreateElement("SoftWare")
        Dim idnode As XmlElement = _xmldoc.CreateElement("Id")
        idnode.InnerText = ty.Id
        dwt.AppendChild(idnode)

        'Dim typenode As XmlElement = _xmldoc.CreateElement("TypeId")
        'typenode.InnerText = ty.TypeId
        'dwt.AppendChild(typenode)

        '重新插入接点
        For Each typeid As String In ty.TypeId
            Dim typenode As XmlElement = _xmldoc.CreateElement("TypeId")
            typenode.InnerText = typeid
            dwt.AppendChild(typenode)
        Next

        Dim namenode As XmlElement = _xmldoc.CreateElement("Name")
        namenode.InnerText = ty.Name
        dwt.AppendChild(namenode)

        Dim path As XmlElement = _xmldoc.CreateElement("Path")
        path.InnerText = ty.FilePath
        dwt.AppendChild(path)

        Dim PathType As XmlElement = _xmldoc.CreateElement("PathType")
        PathType.InnerText = ty.FilePathType
        dwt.AppendChild(PathType)

        Dim iconpath As XmlElement = _xmldoc.CreateElement("IconPath")
        iconpath.InnerText = ty.IconPath
        dwt.AppendChild(iconpath)

        Dim iconpathtype As XmlElement = _xmldoc.CreateElement("IconpathType")
        iconpathtype.InnerText = ty.IconpathType
        dwt.AppendChild(iconpathtype)

        Dim Description As XmlElement = _xmldoc.CreateElement("Description")
        Description.InnerText = ty.Description
        dwt.AppendChild(Description)

        _xmldoc.DocumentElement.AppendChild(dwt)

        _xmldoc.Save(_filePath)

        Return True
    End Function

    Public Function Update(ByVal ty As CSoftWare) As Boolean

        Dim selectNode As XmlNode = _xmldoc.DocumentElement.SelectSingleNode("//SoftWare[Id='" + ty.Id + "']")
        If selectNode IsNot Nothing Then
            selectNode.SelectSingleNode("./Id").InnerText = ty.Id
            'selectNode.SelectSingleNode("./TypeId").InnerText = ty.TypeId
            '删除以前的接点
            Dim typenodes As XmlNodeList = selectNode.SelectNodes("./TypeId")
            For Each typenode As XmlNode In typenodes
                selectNode.RemoveChild(typenode)
            Next
            '重新插入接点
            For Each typeid As String In ty.TypeId
                Dim typenode As XmlElement = _xmldoc.CreateElement("TypeId")
                typenode.InnerText = typeid
                selectNode.AppendChild(typenode)
            Next

            selectNode.SelectSingleNode("./Name").InnerText = ty.Name
            selectNode.SelectSingleNode("./Path").InnerText = ty.FilePath
            selectNode.SelectSingleNode("./PathType").InnerText = ty.FilePathType
            selectNode.SelectSingleNode("./IconPath").InnerText = ty.IconPath
            selectNode.SelectSingleNode("./IconpathType").InnerText = ty.IconpathType
            selectNode.SelectSingleNode("./Description").InnerText = ty.Description
        End If

        _xmldoc.Save(_filePath)

        Return True

    End Function

    Public Function Delete(ByVal id As String) As Boolean

        Dim selectNode As XmlNode = _xmldoc.DocumentElement.SelectSingleNode("//SoftWare[Id='" + id + "']")
        If selectNode IsNot Nothing Then
            _xmldoc.DocumentElement.RemoveChild(selectNode)
        End If

        _xmldoc.Save(_filePath)

        Return True

    End Function


    Public Function DeleteByTypeId(ByVal id As String) As Boolean

        Dim selectNodes As XmlNodeList = _xmldoc.DocumentElement.SelectNodes("//SoftWare[TypeId='" + id + "']")
        For Each selectNode As XmlNode In selectNodes
            _xmldoc.DocumentElement.RemoveChild(selectNode)
        Next

        _xmldoc.Save(_filePath)

        Return True

    End Function

    Public Function DeleteTypeId(ByVal bigtypeId As String) As Boolean

        Dim selectNodes As XmlNodeList = _xmldoc.DocumentElement.SelectNodes("//TypeId")
        For Each selectnode As XmlNode In selectNodes
            If selectnode.InnerText = bigtypeId Then
                selectnode.ParentNode.RemoveChild(selectnode)
            End If
        Next

        _xmldoc.Save(_filePath)

        Return True

    End Function

    Public Function GetById(ByVal id As String) As CSoftWare

        Dim ty As New CSoftWare
        Dim selectNode As XmlNode = _xmldoc.DocumentElement.SelectSingleNode("//SoftWare[Id='" + id + "']")
        If selectNode IsNot Nothing Then
            ty.Id = selectNode.SelectSingleNode("./Id").InnerText
            Dim lstType As New List(Of String)
            Dim typenodes As XmlNodeList = selectNode.SelectNodes("./TypeId")
            For Each nodetype As XmlNode In typenodes
                lstType.Add(nodetype.InnerText)
            Next
            ty.TypeId = lstType
            ty.Name = selectNode.SelectSingleNode("./Name").InnerText
            ty.FilePath = selectNode.SelectSingleNode("./Path").InnerText
            ty.FilePathType = selectNode.SelectSingleNode("./PathType").InnerText
            ty.IconPath = selectNode.SelectSingleNode("./IconPath").InnerText
            ty.IconpathType = selectNode.SelectSingleNode("./IconpathType").InnerText
            ty.Description = selectNode.SelectSingleNode("./Description").InnerText
        End If

        Return ty

    End Function

    Public Function GetAll() As List(Of CSoftWare)
        Dim tys As New List(Of CSoftWare)

        Dim selectnodes As XmlNodeList = _xmldoc.DocumentElement.SelectNodes("//SoftWare")
        For index As Integer = 0 To selectnodes.Count - 1
            Dim selectNode As XmlNode = selectnodes(index)
            Dim ty As New CSoftWare
            ty.Id = selectNode.SelectSingleNode("./Id").InnerText
            Dim lstType As New List(Of String)
            Dim typenodes As XmlNodeList = selectNode.SelectNodes("./TypeId")
            For Each nodetype As XmlNode In typenodes
                lstType.Add(nodetype.InnerText)
            Next
            ty.TypeId = lstType
            ty.Name = selectNode.SelectSingleNode("./Name").InnerText
            ty.FilePath = selectNode.SelectSingleNode("./Path").InnerText
            ty.FilePathType = selectNode.SelectSingleNode("./PathType").InnerText
            ty.IconPath = selectNode.SelectSingleNode("./IconPath").InnerText
            ty.IconpathType = selectNode.SelectSingleNode("./IconpathType").InnerText
            ty.Description = selectNode.SelectSingleNode("./Description").InnerText
            tys.Add(ty)
        Next

        Return tys

    End Function

    Public Function GetByTypeId(ByVal typeId As String) As List(Of CSoftWare)
        Dim tys As New List(Of CSoftWare)

        Dim selectnodes As XmlNodeList = _xmldoc.DocumentElement.SelectNodes("//SoftWare[TypeId='" + typeId + "']")
        For index As Integer = 0 To selectnodes.Count - 1
            Dim selectNode As XmlNode = selectnodes(index)
            Dim ty As New CSoftWare
            ty.Id = selectNode.SelectSingleNode("./Id").InnerText
            Dim lstType As New List(Of String)
            Dim typenodes As XmlNodeList = selectNode.SelectNodes("./TypeId")
            For Each nodetype As XmlNode In typenodes
                lstType.Add(nodetype.InnerText)
            Next
            ty.TypeId = lstType
            ty.Name = selectNode.SelectSingleNode("./Name").InnerText
            ty.FilePath = selectNode.SelectSingleNode("./Path").InnerText
            ty.FilePathType = selectNode.SelectSingleNode("./PathType").InnerText
            ty.IconPath = selectNode.SelectSingleNode("./IconPath").InnerText
            ty.IconpathType = selectNode.SelectSingleNode("./IconpathType").InnerText
            ty.Description = selectNode.SelectSingleNode("./Description").InnerText
            tys.Add(ty)
        Next

        Return tys

    End Function

    Public Function GetMaxid() As String

        Dim selectnodes As XmlNodeList = _xmldoc.DocumentElement.SelectNodes("//Id")
        Dim maxid As Integer = 0
        For index As Integer = 0 To selectnodes.Count - 1
            If CInt(selectnodes(index).InnerText) > maxid Then
                maxid = CInt(selectnodes(index).InnerText)
            End If
        Next

        Return CStr(maxid + 1).PadLeft(4, "0"(0))

    End Function

End Class
